<?php
namespace app\common\service;

use app\common\model\User;
use think\db\exception\ModelNotFoundException;

class AuthService
{
    /**
     * 用户登录
     * @param $username
     * @param $password
     * @throws ModelNotFoundException
     */
    public function login($username, $password): User
    {
        $user = User::where('username', $username)->find();
        if (!$user) {
            throw new ModelNotFoundException('用户不存在');
        }

        if (!password_verify($password, $user->password)) {
            throw new \RuntimeException('密码错误');
        }

        if ((int)$user->status !== 1) {
            throw new \RuntimeException('用户被冻结');
        }

        // 可以在这里处理登录后的行为，如更新登录时间、记录日志等
        return $user;
    }
}